import { defineConfig, build } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
// 最新 node 核心包的导入写法
import { fileURLToPath } from 'node:url';
import path, { dirname } from 'node:path';
import fs from 'node:fs';
import fsExra from 'fs-extra';
// 获取 __filename 的 ESM 写法
// const __filename = fileURLToPath(import.meta.url);

// 获取 __dirname 的 ESM 写法
const __dirname = dirname(fileURLToPath(import.meta.url));

// 打包入口文件夹
const entryDir = path.resolve(__dirname, '../packages');

// 出口文件夹
const outDir = path.resolve(__dirname, '../lib');
// vite 基础配置

const baseConfig = defineConfig({
    configFile: false,
    publicDir: false,
    plugins: [vue(), vueJsx()],
})

// rollup 配置
const rollupOptions = {
    external: ['vue', 'vue-router'],
    output: {
        globals: {
            vue: 'Vue'
        }
    }
}

// 全量打包
const buildAll = async () => {
    await build({
        ...baseConfig,
        build: {
            rollupOptions,
            lib: {
                entry: path.resolve(entryDir, 'index.ts'),
                name: 'index',
                fileName: 'index',
                formats: ['es', 'umd'],
            },
            outDir
        }
    })
    const dtsStr = `import {App} from 'vue';declare const _default: {install(vue: App): void;};export default _default;`
    fsExra.outputFile(
        path.resolve(outDir, `index.d.ts`), dtsStr, 'UTF-8');
}


// 单体打包
const buildSingle = async (name) => {
    console.log('name', name);
    await build({
        ...baseConfig,
        build: {
            lib: {
                entry: path.resolve(entryDir, name),
                name: 'index',
                fileName: 'index',
                formats: ['es', 'umd']
            },
            outDir: path.resolve(outDir, name)
        }
    })
}

const createPackageJson = (name) => {
    const fileStr = `
    {
        "name": "${name}",
        "main": "index.umd.js",
        "module": "index.js",
        "style": "style.css",
        "type": "index.d.ts",
        "exports": {
            ".": {
                "types": "../index.d.ts",
                "node": {
                    "import": {
                        "production": "./index.js",
                        "development": "./index.js",
                        "default": "./index.js"
                    },
                    "require": {
                        "production": "./index.umd.cjs",
                        "development": "./index.umd.cjs",
                        "default": "./index.js"
                    }
                },
                "import": "./index.js",
                "require": "./index.js"
            },
        },
    }
    `;
    fsExra.outputFile(
        path.resolve(outDir, `${name}/package.json`), fileStr, 'UTF-8');
    const dtsStr = `import {App} from 'vue';declare const _default: {install(vue: App): void;};export default _default;`
    fsExra.outputFile(
        path.resolve(outDir, `${name}/index.d.ts`), dtsStr, 'UTF-8');
}

const buildLib = async () => {
    await buildAll();

    const components = fs.readdirSync(entryDir).filter(name => {
        const componentDir = path.resolve(entryDir, name);
        const isDir = fs.lstatSync(componentDir).isDirectory();
        return isDir && fs.readdirSync(componentDir).includes('index.ts');
    })

    for (const name of components) {
        await buildSingle(name);
        createPackageJson(name);
    }
}

buildLib();


